package com.resou.reader.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.resou.reader.R;
import com.resou.reader.utils.log.DLog;

/**
 * Created by ${Garrett} on 2018/9/30.
 * Contact me krouky@outlook.com
 */
public class DividerItemDecoration extends RecyclerView.ItemDecoration {

    private float dividerHeight;
    private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

    public DividerItemDecoration(Context context) {
        dividerHeight = context.getResources().getDimension(R.dimen.qb_px_2);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(context.getResources().getDimension(R.dimen.qb_px_2));
        mPaint.setColor(0x808a8a8a);
    }

    private static final String TAG = "DividerItem-App";
    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        Log.d(TAG, "getItemOffsets: ");
    }

    @Override
    public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.onDraw(c, parent, state);
        Log.d(TAG, "onDraw: ");
        float left = parent.getPaddingStart();
        float right = parent.getWidth() - parent.getPaddingEnd();
        int childCount = parent.getChildCount();
        DLog.d("DividerItemDecoration", "childCount:" + childCount);
        for (int i = 0; i < childCount - 1; i++) {
            View view = parent.getChildAt(i);
            float top = view.getBottom();
            float bottom = top + dividerHeight;
            c.drawLine(left, top, right, top, mPaint);
        }
    }
}
